import type {RouteRecordRaw} from "vue-router";
import router from "../router/index";

type IRoute = RouteRecordRaw & {id?:string};

// 定义全部路由：Admin下的子一级。
const allRoutes:IRoute[] = [
    {
        id:"1",
        name:"/",
        path: "",
        redirect: "/Adman/Home"
    },
    {
        id:"1",
        name:"Home",
        path: "/Adman/Home",
        component: () => import("../views/Adman/Home/Index.vue")
    },
    {
        id:"2-1",
        name:"Goods",
        path: "/Adman/Goods",
        component: () => import("../views/Adman/Shops/Goods.vue")
    },
    {
        id:"2-2",
        name:"Shoplist",
        path: "/Adman/Shoplist",
        component: () => import("../views/Adman/Shops/Shoplist.vue")
    },
    {
        id:"3-1",
        name:"List",
        path: "/Adman/List",
        component: () => import("../views/Adman/Banner/List.vue")
    },
    {
        id:"3-2",
        name:"Add",
        path: "/Adman/Add",
        component: () => import("../views/Adman/Banner/Add.vue")
    },
    {
        id:"4-1",
        name:"Administrator",
        path: "/Adman/Administrator",
        component: () => import("../views/Adman/Operation/Administrator.vue")
    },
    {
        id:"4-2",
        name:"Addition",
        path: "/Adman/Addition",
        component: () => import("../views/Adman/Operation/Addition.vue")
    },
   
    {
        id:"5-2",
        name:"Charts",
        path: "/Adman/Charts",
        component: () => import("../views/Adman/Data/Charts.vue")
    },
    
    {
        id:"6",
        name:"User",
        path: "/Adman/User",
        component: () => import("../views/Adman/User/Index.vue")
    },
    
    {
        id:"8",
        name:"Order",
        path: "/Adman/Order",
        component: () => import("../views/Adman/Order/Index.vue")
    }
]      



export default function getRoutes(){
    // 1、获取当前用户的权限
    let checkedKeys:string[] = [];
    const grants =  sessionStorage.getItem("grants");
    if(grants){
        checkedKeys = JSON.parse(grants);
    }
    if(checkedKeys.length==0){
        checkedKeys = ['1','2','2-1','2-2','2-3','3','3-1','3-2','4','4-1','4-2','5','5-1','5-2','6','6-1','7','7-1','3-1-R','3-1-U','3-1-D','8','8-1'];
    }

    // 2、获取当前用户的路由配置

    allRoutes.forEach(item=>{
        if(checkedKeys.includes(item.id as string)){
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            
            let curRouter = {...item}
            let arr = ["0","0","0","0"];
            if(checkedKeys.includes(item.id+"-C")){
                arr[0]="1";
            }
            if(checkedKeys.includes(item.id+"-R")){
                arr[1]="1";
            }
            if(checkedKeys.includes(item.id+"-U")){
                arr[2]="1";
            }
            if(checkedKeys.includes(item.id+"-D")){
                arr[3]="1";
            }
            curRouter.meta={
                crud:arr.join("")
            }
            router.addRoute("Adman",curRouter);

        }
    })


    console.log("router.getRoutes()",router.getRoutes());
}

export const  clearCurrentRoutes = ()=>{
   // 1、获取当前用户的权限
   let checkedKeys:string[] = [];
   const grants =  sessionStorage.getItem("grants");
   if(grants){
       checkedKeys = JSON.parse(grants);
   }
   if(checkedKeys.length===0){
       checkedKeys = ['1','2','2-1','2-2','2-3','3','3-1','3-2','4','4-1','4-2','5','5-1','5-2','6','6-1','7','7-1','8','8-1'];
   }

   // 2、删除当前用户的路由配置

   allRoutes.forEach(item=>{
       if(checkedKeys.includes(item.id as string)){
           // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
        //    router.addRoute("Admin",{...item});
          router.removeRoute(item.name as string);
       }
   })

}